package com.lening.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lening.yygh.hosp.repository.DepartmentRepository;
import com.lening.yygh.hosp.service.DepartmentService;
import com.lening.yygh.model.hosp.Department;
import com.lening.yygh.vo.hosp.DepartmentQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartmentRepository departmentRepository;


    /**
     * 我们昨天学习的MongoDB的条件查询加分页
     */
    @Override
    public Page<Department> selectPage(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        //按照创建时间来排序
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        //第一页
        Pageable pageable = PageRequest.of(page-1, limit, sort);

        //查询没有删除的，从医院传递过来的是一个vo，我们需要把vo复制到实体类中
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);

        //创建匹配器，即如何使用查询条件，忽略大小小去匹配实体类里面不为空的额字段
        //共计三个，是否删除，医院编号和科室编号
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

        //创建实例
        Example<Department> example = Example.of(department, matcher);
        Page<Department> pages = departmentRepository.findAll(example, pageable);
        return pages;
    }

    @Override
    public void save(Map<String, Object> paramMap) {

        Department department = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Department.class);
        Department targetDepartment = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());

        if(null != targetDepartment) {
            //copy不为null的值，该方法为自定义方法
            department.setId(targetDepartment.getId());
            department.setUpdateTime(new Date());
            departmentRepository.save(department);

        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public void remove(String hoscode, String depcode) {
        /**
         * 我们直接删除了，不再判断了，按常理来说，要是删除科室大类，就需要把他下面的小类全部删除
         * 要是删除科室小类，还需要判断这个小类是不是该科室的最后一个小类了，要是的话，需要把科室小类对应的大类也删除
         * 递归思想
         */
        /**
         * 直接写删除，使用医院编号和部门标号也能删除，但是我们已经有按照医院编号和部门(就是科室)
         * 编号，查询科室的方法了，我们直接调用，然后就有科室的id了，然后我们直接使用id删除
         */

        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department!=null&&department.getId()!=null){
            departmentRepository.deleteById(department.getId());
        }

    }
}
